package chapter1;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @author ycj
 * @version 1.0
 * @description: TODO
 * @date 2022/5/16
 */

//多线程文件下载
public class Source1_9 {


    @Test
    public void TestSingleThread(){
        String fromFile="D:\\readBookSource\\java多线程编程实战指南\\src\\chapter1\\fromFile\\";
        String toFile="D:\\readBookSource\\java多线程编程实战指南\\src\\chapter1\\toFile";
        long startTime=System.currentTimeMillis();
        for(int i=1;i<=3;i++){
            BufferedReader bufferedReader=null;
            BufferedWriter bufferedWriter=null;
            String lineSeparator = System.lineSeparator();
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile+i+".txt")));
                bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile+"\\"+i+".txt")));
                StringBuilder stringBuilder = new StringBuilder();
                String str;
                while((str=bufferedReader.readLine())!=null){
                    stringBuilder.append(str).append(lineSeparator);
                }

                bufferedWriter.write(stringBuilder.toString());
                bufferedWriter.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedReader.close();
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long endTime=System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName()+"耗时="+(endTime-startTime));
        //单个main线程 main耗时=6
    }

    @Test
    public void TestMultiplyThread(){
        String fromFile="D:\\readBookSource\\java多线程编程实战指南\\src\\chapter1\\fromFile\\";
        String toFile="D:\\readBookSource\\java多线程编程实战指南\\src\\chapter1\\toFile";
        Thread thread;
        for(int i=1;i<=3;i++){
            thread= new Thread(new FileDownLoad(fromFile+i+".txt", toFile, i),"A"+i);
            thread.start();
        }
        //A3耗时=2
        //A2耗时=3
        //A1耗时=3
        //多线程情况下只要3s
    }
}

class FileDownLoad implements Runnable{
    private String fromFileUrl;
    private String toFileUrl;
    private int id;

    public FileDownLoad(String fromFileUrl, String toFileUrl, int id) {
        this.fromFileUrl = fromFileUrl;
        this.toFileUrl = toFileUrl;
        this.id = id;
    }

    @Override
    public void run() {
        //默认传入文件路径正确
        File fromFile = new File(fromFileUrl);
        File toFile = new File(toFileUrl);
        if(fromFile.isFile()&&toFile.isDirectory()){
            long startTime=System.currentTimeMillis();
            BufferedReader bufferedReader=null;
            BufferedWriter bufferedWriter=null;
            String lineSeparator = System.lineSeparator();
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile)));
                bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile+"\\"+id+".txt")));
                StringBuilder stringBuilder = new StringBuilder();
                String str;
                while((str=bufferedReader.readLine())!=null){
                    stringBuilder.append(str).append(lineSeparator);
                }

                bufferedWriter.write(stringBuilder.toString());
                bufferedWriter.flush();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedReader.close();
                    bufferedWriter.close();
                    long endTime=System.currentTimeMillis();
                    System.out.println(Thread.currentThread().getName()+"耗时="+(endTime-startTime));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
